using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace WPCoco.Transitions
{
    public class FadeTransition : Transition
    {
        public FadeTransition(Scene targetScane, Color color, float duration)
            : base(targetScane)
        {
            renderTarget = new RenderTarget2D(Screen.CurrentGraphicsDevice,
                                              Screen.Width, Screen.Height,
                                              false,
                                              SurfaceFormat.Color,
                                              Screen.CurrentGraphicsDevice.PresentationParameters.DepthStencilFormat,
                                              Screen.CurrentGraphicsDevice.PresentationParameters.MultiSampleCount,
                                              RenderTargetUsage.PreserveContents);
            Duration = duration;
            Color = color;
        }

        public FadeTransition(Scene targetScane, float duration)
            : this(targetScane, Color.Black, duration)
        {

        }

        Color color;

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        float duration;

        public float Duration
        {
            get { return duration; }
            private set { duration = value; }
        }

        RenderTarget2D renderTarget;

        bool flag, flag2;
        Color usedColor; float usedDuration;
            
        public override void Initialize()
        {
            base.Initialize();
            flag = true; flag2 = true; usedColor = color; usedDuration = duration;
        }

        float startTime;

        public override void Draw(GameTime gameTime)
        {
            if (flag) 
            {
                flag = false;
                startTime = (float)gameTime.TotalGameTime.TotalSeconds;
                RaiseExitScene(LastScene);
            }
            float time = (float)gameTime.TotalGameTime.TotalSeconds;
            float elapsed = time - startTime;
            Debug.WriteLine(elapsed);

            Screen.CurrentGraphicsDevice.SetRenderTarget(renderTarget);
            Screen.CurrentGraphicsDevice.Clear(Color.Black);
            float a = 1;
            if (elapsed < usedDuration / 2)
            {
                if (LastScene != null)
                {
                    WorldMatrix.Shared.Begin();
                    LastScene.Draw(gameTime);
                    WorldMatrix.Shared.End();
                }
                a = 1 - 2 * elapsed / usedDuration;
            }
            else 
            {
                a = 2 * elapsed / usedDuration - 1;
                if (flag2) 
                {
                    flag2 = false;
                    RaiseEnterScene(TargetScene);
                }
                if (TargetScene != null)
                {
                    WorldMatrix.Shared.Begin();
                    TargetScene.Draw(gameTime);
                    WorldMatrix.Shared.End();
                }
            }

            CachedSpriteBatch spriteBatch = SpriteBatchCache.GetVolatile();

            Screen.CurrentGraphicsDevice.SetRenderTarget(null);
            Screen.CurrentGraphicsDevice.Clear(usedColor);
            spriteBatch.Begin();
                spriteBatch.Draw(renderTarget, Vector2.Zero, new Color(a, a, a, a));
            spriteBatch.End();

            if (elapsed > duration)
            {
                RaiseEndTransition();
            }
        }
    }
}
